<?php
/**
 * 
 * Domain model for creating and sending invitations
 * 
 * @category Abovo
 * 
 * @package Abovo_Invites
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 */

/**
 * 
 * Domain model for creating and sending invitations
 * 
 * Uses Abovo_Model_Invites, Solar_Mail and Abovo_Model_Users
 * to create, send and approve invitations
 * 
 * @category Abovo
 * 
 * @package Abovo_Invite
 * 
 */
class Abovo_Invite extends Solar_Base {
    
    /**
     * 
     * Config keys
     * 
     * `url`
     * : (string) A uri spec for creating a link to
     *   invitation approval page. This is run through sprintf()
     *   to place the invitation hash into the url.
     * 
     * @var array
     * 
     */
    protected $_Abovo_Invite = array(
        'url'  => 'system/user/approve/%s',
    );
    
    /**
     * 
     * Invitation table model
     * 
     * @var Abovo_Model_Invites
     * 
     */
    public $invites;
    
    /**
     * 
     * Constructor
     * 
     * @return void
     * 
     */
    public function __construct($config = array())
    {
        parent::__construct($config);
        $this->invites = Solar::factory('Abovo_Model_Invites');
    }
    
    /**
     * 
     * Creates and sends an invitation
     * 
     * @return void
     * 
     */
    public function send($email, $msg)
    {
        // get user handle
        $handle = Solar::registry('user')->auth->handle;
        
        // create invitation (insert to table)
        $invite = $this->invites->create($email, $handle);
        
        // send invitation
        $this->_mail($invite['email'], $invite['hash'], $invite['handle'], $msg);
    }
    
    /**
     * 
     * Sends the actial invite
     * 
     * @return void
     * 
     */
    private function _mail($email, $hash, $handle, $msg = '')
    {
        // get users model
        $users = Solar::factory('Abovo_Model_Contacts_Users');
        
        // fetch contact info for handle
        $contact = $users->fetchByHandle($handle);
        
        // who is the sender
        $name = "{$contact->firstname} {$contact->lastname}";
        
        // put the hash into the url
        $spec = sprintf($this->_config['url'], $hash);
        
        // create url
        $uri = Solar::factory('Solar_Uri_Action');
        $url = $uri->quick($spec, true);
        
        // mail body. Fill the name and url placeholders
        $body = sprintf(
            $this->locale('TEXT_INVITE_BODY'),
            $name,
            $url
        );
        
        $mail = Solar::factory('Solar_Mail_Message');
        
        // build mail and send
        $mail->setFrom($contact->email, $name)
             ->addTo($email)
             ->setSubject($this->locale('TEXT_INVITE_SUBJECT'))
             ->setText($msg . $body)
             ->send();
    }
    
    /**
     * 
     * Creates a form for sending an invitation
     * 
     * @return Solar_Form
     * 
     */
    public function form()
    {
        $form = Solar::factory('Solar_Form');
        
        // form elements
        $elements = array(
            'email' => array(
                'label'   => $this->locale('LABEL_EMAIL'),
                'type'    => 'text',
                'valid'   => 'email',
                'attribs' => array(
                    'size' => 32,
                )
            ),
            'message' => array(
                'type'    => 'textarea',
                'label'   => 'Message',
                'value'   => $this->locale('TEXT_INVITE_WELCOME'),
                'attribs' => array(
                    'cols' => 40,
                    'rows' => 5,
                )
            ),
        );
        
        // set elements
        $form->setElements($elements);
        
        // return form object
        return $form;
    }
}
